package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IAsk;
import com.wbao.dianniu.listener.IAskListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

public class AskManager implements IAsk {

    private IAskListener mListener;
    private Context mContext;

    public AskManager(Context context) {
        mContext = context;
    }


    /**
     * @param accountId
     * @param type
     * @param content
     * @param questImgs
     * @param showAliasName
     * @param address
     * @param noticeJson
     */
    @Override
    public void ask(int accountId, int type, String content, String questImgs, int showAliasName, String address, String noticeJson,Integer anonymous) {
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(GlobalContext.getCacheToken());
            Command command = new Command();
            command.addAction(Action.ACTION_ACCOUNT_QUEST_ADD)
                    .addArgs("accountId", accountId)
                    .addArgs("type", type)
                    .addArgs("content", content)
                    .addArgs("questImgs", questImgs)
                    .addArgs("showAliasName", showAliasName)
                    .addArgs("address", address)
                    .addArgs("noticeJson", noticeJson)
                    .addArgs("anonymous",anonymous);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                QaResponse addData = JsonUtil.toObject(obj, QaResponse.class);
                // 通知界面
                if (null != mListener)
                    mListener.onAskSuccess(addData);
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != mListener)
                    mListener.onAskFailure(errorCode, msg);
            }
        });
    }

    @Override
    public boolean addAskListener(IAskListener listener) {
        mListener = listener;
        return mListener != null;
    }

    @Override
    public boolean removeAskListener(IAskListener listener) {
        if (null == listener)
            return false;
        mListener = null;
        return true;
    }

}
